var userAgent = navigator.userAgent.toLowerCase();
var ua_match = /(trident)(?:.*rv:([\w.]+))?/.exec(userAgent) || /(msie) ([\w.]+)/.exec(userAgent);
var is_ie = ua_match && (ua_match[1] == 'trident' || ua_match[1] == 'msie') ? true : false;
//添加签章功能
function addSeal(e,seal_id){
    var WebObj = document.getElementById("DWebSignSeal");
    var id = e.parent().find('input[type=hidden]').attr('name');
    var sid = 'SIGN_POS_'+id;
    try {
        if (WebObj.FindSeal(id + "_seal", 2) != "") {
            alert("您已经签章，请先删除已有签章！");
            return;
        }

        var str = SetStore(id);
        WebObj.SetPosition(0,0,sid);
        if(typeof seal_id=="undefined")
            show_seal(id,"addSeal");
        else {
            var url = location.origin+"/seal/querySealById?sealId="+seal_id;
            var sealid = document.all.DWebSignSeal.AddSeal(url, id+"_seal");
            if (sealid == "") {
                alert("盖章失败！");
            }
        }
        document.all.SetSealSignData(id+"_seal",str);
        document.all.SetMenuItem(id+"_seal",261);
        document.all.LockSealPosition(sealid);
    }catch(e){}
}
function addSeal1(id,seal_id){
    var WebObj = document.getElementById("DWebSignSeal");
    var id = id;
    var sid = 'SIGN_POS_'+id;
    try {
        if (WebObj.FindSeal(id + "_seal", 2) != "") {
            alert("您已经签章，请先删除已有签章！");
            return;
        }

        var str = SetStore(id);
        WebObj.SetPosition(0,0,sid);
        if(typeof seal_id=="undefined")
            show_seal(id,"addSeal");
        else
        {
            var url = location.origin+"/seal/querySealById?sealId="+seal_id;
            var sealid = document.all.DWebSignSeal.AddSeal(url, id+"_seal");
            if (sealid == "") {
                alert("盖章失败！");
            }
        }
        document.all.SetSealSignData(id+"_seal",str);
        document.all.SetMenuItem(id+"_seal",261);
        document.all.LockSealPosition(sealid);
    }catch(e){}

}
//添加手写功能
function handWrite(e){
    var WebObj = document.getElementById("DWebSignSeal");
    var id = e.parent().find('input[type=hidden]').attr('name');
    var sid = 'SIGN_POS_'+id;
    var color = parseInt(e.attr('color'));
    try {
        if(WebObj.FindSeal(id+"_hw",2)!="")
        {
            alert("您已经签章，请先删除已有签章！");
            return;
        }
        var str = SetStore(id);
        WebObj.SetPosition(0,0,sid);
        var obj_name = document.all.DWebSignSeal.HandWrite(0,color,id+"_hw");
        WebObj.SetSealSignData(id+"_hw",str);
        // WebObj.SetMenuItem(item+"_hw",261);
        var  strSealName = WebObj.LockSealPosition(id+"_hw");
    }catch(e){
        if (sealid == "") {
            alert("手写失败！");
        }else{

        }
        return sealid;
    }
}
//加载签章锁定字段功能START
function SetStore(item) {
    try {
        var str= GetDataStr(item);
        document.all.DWebSignSeal.SetSignData("-");
        document.all.DWebSignSeal.SetSignData("+DATA:" + str);
    }
    catch(e) {}
    return str;
}
function GetDataStr(item){
    var sign_check = getCheckStr(item);

    if(typeof item == 'undefined') {
        return;
    }

    var str="";
    var separator = "::";  // 分隔符
    // eval("var TO_VAL=sign_check."+item+";");

    sign_check = sign_check.substr(0,sign_check.length-1);

    var check_array = new Array();
    var check_array1 = new Array();

    check_array = sign_check.split(",");

    for(cCount=0; cCount<check_array.length; cCount++) {
        check_array1[cCount] = check_array[cCount].split(":");

        if(item.indexOf(check_array1[cCount][0]) != -1) {
            TO_VAL = check_array1[cCount][1];
        }
        else if(item.indexOf(check_array1[cCount][0]) == -1) {
            TO_VAL = TO_VAL + "," + check_array[cCount]
        }
    }

    if(TO_VAL) {
        var item_array = TO_VAL.split(",");
        for (i=0; i < item_array.length; i++) {
            var MyValue="";
            var obj = document.getElementById(item_array[i]);
            if(obj != undefined) {
                if(obj.type == undefined) {
                    if(obj[0].type == "radio") {
                        var obj_len = obj.length;
                        for(rCount=0; rCount<obj_len; rCount++) {
                            if(obj[rCount].checked) {
                                MyValue = obj[rCount].value;
                            }
                        }
                    }
                }
                else if(obj.type == "checkbox") {

                    if(obj.checked == true)
                        MyValue = "on";
                    else
                        MyValue = "";
                }
                else {
                    MyValue=obj.value;
                }

                if(obj.type == undefined) {
                    if(obj[0].type == "radio") {
                        str += obj[0].name + "separator" + MyValue + "\n";
                    }
                }
                else {
                    str += obj.name + "separator" + MyValue + "\n";
                }
            }
        }
    }
    return str;
}
function getCheckStr(item) {
    var sign_check = '';
    var sign_length = $('div[class="websign"]').length;
    for(count = 0; count < sign_length; count++) {
        if($('div[class="websign"]:eq('+count+')').attr("sign_check")) {
            if($('div[class="websign"]:eq('+count+')').attr("sign_check").indexOf(item+":") != -1) {
                sign_check += $('div[class="websign"]:eq('+count+')').attr("sign_check");
            }
        }
    }
    return sign_check;
}
//加载签章锁定字段功能END
function WebSign_Submit(sign_arr){
    if(is_ie){
        try {
            var sign_val;
            for(var i=0;i<sign_arr.length;i++) {
                if(sign_arr[i]!="") {
                    var objName_hw = DWebSignSeal.FindSeal(sign_arr[i]+"_hw",2);
                    var objName_seal = DWebSignSeal.FindSeal(sign_arr[i]+"_seal",2);

                    if(objName_hw=="" && objName_seal==""){
                        sign_val="";
                    }
                    else{
                        sign_val='DATA_HW:'+DWebSignSeal.GetStoreDataEx(sign_arr[i]+"_hw")+'DATA_SEAL:'+DWebSignSeal.GetStoreDataEx(sign_arr[i]+"_seal");
                    }

                    $('input[name='+ sign_arr[i] +']').val(sign_val);
                }
            }
        }catch(e) {}
    }

}
//加载点聚、签章数据
function LoadSignData(sign_arr) {
    try {
        /*************判断点聚web签章是否被可选组件选中start***********************************/
        if($('#module').val() != ''){
            if(matchString($('#module').val(),'2')){
                $.ajax({
                    type:'post',
                    url:'/syspara/selectPassword',
                    data:{},
                    dataType:'json',
                    async:false,
                    success:function(json){
                        if(json.object.paraValue != ''){
                            DWebSignSeal.SetSealComment("SET_ABOUT_TIPS",0,0,json.object.paraValue);
                        }
                    }
                })
            }
        }
        /*************判断点聚web签章是否被可选组件选中end***********************************/

        for(var i=0;i<sign_arr.length;i++) {
            var str = $('input[name='+ sign_arr[i] +']').val();
            if(str != ""&&str.indexOf('DATA_HW:') > -1&&str.indexOf('DATA_SEAL:') > -1&&str.split('DATA_HW:')[1] != '') {
                var hw = str.split('DATA_HW:')[1].split('DATA_SEAL:')[0];
                var seal = str.split('DATA_HW:')[1].split('DATA_SEAL:')[1];

                if(hw != ''){
                    DWebSignSeal.SetStoreData(hw);
                }
                if(seal != ''){
                    DWebSignSeal.SetStoreData(seal);
                }
            }else if(str != ""){
                DWebSignSeal.SetStoreData(str);
            }
        }
        DWebSignSeal.ShowWebSeals();

        var str= "";
        var strObjectName ;
        strObjectName = DWebSignSeal.FindSeal("",0);
        while(strObjectName  != "") {
            var item_str = "";
            if(strObjectName.indexOf("_hw")>0)
                item_str = strObjectName.substring(0,strObjectName.indexOf("_hw"));
            else if(strObjectName.indexOf("_seal")>0)
                item_str = strObjectName.substring(0,strObjectName.indexOf("_seal"));
            else if(strObjectName.indexOf("SIGN_INFO")<0) //兼容通达的旧数据
            {
                item_str = strObjectName;
            }
            if(item_str!="") {
                var item_obj = $("#"+item_str);
                str = GetDataStr(item_str);
                DWebSignSeal.SetSealSignData(strObjectName,str);
                if(item_obj.attr('disabled') != undefined||item_obj.attr('disabled') == 'disabled') {
                    DWebSignSeal.SetMenuItem(strObjectName,4);
                }
                else {
                    DWebSignSeal.SetMenuItem(strObjectName,261);
                }

            }
            strObjectName = DWebSignSeal.FindSeal(strObjectName,0);
        }
        // 设置web端签章手写用户名名称为 用户名加角色 改为只通过用户名
        // DWebSignSeal.SetCurrUser(workForm.tool.MacrosData.data.sYS_USERNAME+"["+ workForm.tool.MacrosData.data.sYS_USERPRIV +"]");
        DWebSignSeal.SetCurrUser(workForm.tool.MacrosData.data.sYS_USERNAME);
        DWebSignSeal.LockSealPosition("");
    }
    catch(e) {
        // alert("手写签章加载失败，请检查控件是否正确安装！");
    }
    //加载完成标识
    SignLoadFlag = true;
}
//打开签章页面
function show_seal(item){
    var URL = "/common/selectSeal?ITEM=" + item ;
    $.popWindow(URL);
}
